"""
@author: zjc
@file: 回合制对战游戏.py
@time: 2022/12/2 15:11
"""
import time
class Role():
    def __init__(self,name,sex,equip,HP=100):
        self.name=name
        self.sex=sex
        self.equip=equip
        self.HP=HP          #角色基础血量
    def attack(self,role):
        print("普通攻击")
        if role.equip==0:
            role.HP-=5
        else:
            role.HP-=10

    def defense(self,role):
        if role.equip==1 and role.HP<=100:
            role.HP+=8
        else:
            role.HP+=3
class Room():
    def __init__(self,name):
        self.name=name
        self.member=[]
    def add_room(self,role):
        if role.room==None:
            if len(self.member)<2:
                self.member.append(role)
                role.room=self.name
            else:
                print(f"{self.name}房间以满")
        else:
            print("已加入房间")

role={}
room_dict={}
while True:
    print("1.创建角色\n2.创建房间\n3.加入房间\n4.开始战斗\n5.退出")
    op=input("请输入选择:")
    if op=="1":
        name = input("请输入角色名:")
        sex = input("请输入性别:")
        equip = int(input("请输入装备编号:"))
        role[name]=Role(name,sex,equip)
    elif op=="2":
        name=input("请输入房间的名字:")
        room_dict[name]=Room(name)
    elif op=="3":
        print(f"角色列表{role.keys()},房间列表{room_dict.keys()}")
        role_name=input("请输入要加入的角色:")
        room_name=input("请输入要加入的房间名:")
        room_dict[room_name].member.append(role[role_name])
    elif op=="4":
        print(f"房间列表{room_dict.keys()}")
        room_name=input("请选择房间:")
        print(room_dict[room_name].member[0])
        if len(room_dict[room_name].member)==2:
            i=1
            a=room_dict[room_name].member[0]
            b=room_dict[room_name].member[1]
            while True:
                print(f"第----<{i}>----回合".center(33))
                i = i + 1
                print(f"{a.name}攻击------>", end="")
                a.attack(b)
                b.defense(b)
                if a.HP <= 0 or b.HP <= 0:
                    winner = a.name if a.HP > 0 else \
                        b.name
                    print(f"{winner}----win----".center(40, "_"))
                    break

                print(f"{b.name}攻击------>", end="")
                b.attack(a)
                a.defense(a)
                if a.HP <= 0 or b.HP <= 0:
                    winner = a.name if a.HP > 0 else \
                        b.name
                    print(f"{winner}----win----".center(40, "_"))
                    break
                print(f"{a.name}:hp--{a.HP}    vs   "
                      f"{b.name}:hp--{b.HP}")
                time.sleep(0.5)
                print("_" * 40)
    elif op=="5":
        print("退出对战")
        break













